﻿using DearlerPlatform.Domain;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace DearlerPlatfrom.Extensions
{
    public static class RepositoryRegisterExtensions
    {
        public static IServiceCollection RepositoryRegister(this IServiceCollection services)
        {
            var asmCore = Assembly.Load("DearlerPlatfrom.Core");
            var implementationType = asmCore.GetTypes().FirstOrDefault(m => m.Name == "Repository`1");//找有一个泛型参数的
            var interfaceType = asmCore.GetTypes().FirstOrDefault(m => m.Name == "IRepository`1"); //implementationType?.GetInterface("IRepository`1").GetGenericTypeDefinition();
            if (interfaceType != null && implementationType != null)
            {
                services.AddTransient(interfaceType, implementationType);
            }
            return services;
        }

        public static IServiceCollection ServicesRegister(this IServiceCollection services)
        {
            var asmCore = Assembly.Load("DearlerPlatfrom.Service");
            var implementationTypes = asmCore.GetTypes().Where(m => m.IsAssignableTo(typeof(IocTag))
            && !m.IsAbstract && !m.IsInterface);

            foreach (var implementationType in implementationTypes)
            {
                var interfaceType = implementationType.GetInterfaces().Where(m => m != typeof(IocTag)).FirstOrDefault();
                services.AddTransient(interfaceType, implementationType);
            }
            return services;

        }

    }
}
